package com.example.review;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * ClassName: CustomProducer
 * Package: com.example.review
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-08-05
 * Time: 9:54
 */

//自定义生产者
public class CustomProducer {

    public static void main(String[] args) {

        //0.创建配置信息
        Properties properties = new Properties();

        //必须配置的参数
        //连接集群的方式
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"hadoop102:9092");

        //key val的序列化器
        //生产者是序列化器 消费者是反序列化器 StringSerializer是这个Serializer
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());

        //1.创建生产者
        KafkaProducer<String, String> kafkaProducer =
                new KafkaProducer<String, String>(properties);


        //2.发送数据 配置参数的时候 指定主题和分区
        for (int i = 0; i < 5; i++) {
            kafkaProducer.send(new ProducerRecord<>("first","Java" + i));
        }

        //关闭资源 才能消费到数据
        kafkaProducer.close();
    }

}
